#define DBC_CHECK_PRECONDITION

using System;
using System.Xml;

using DesignByContract;

using Outcoding.GraphGenerator.Entities;

namespace Outcoding.GraphGenerator.XmlBuilders {
    public class LegendXmlBuilder : IXmlBuilder {

        private Legend mResultObject = null;

        public object ResultObject {
            get {
                return mResultObject;
            }
        }

        public LegendXmlBuilder(XmlNode legendXmlNode) {
            Check.Require(legendXmlNode != null, "Xml source node cannot be null");
            Check.Require(legendXmlNode.Name.CompareTo(XmlTagNames.legend.ToString()) == 0, "A Legend Xml node must be supplied.");
            this.BuildObject(legendXmlNode);
        }

        private void BuildObject(XmlNode legendXmlNode) {
            mResultObject = new Legend();
            try {
                if (legendXmlNode.Attributes["position"] != null)
                    mResultObject.Position = ParsePositionAttribute(legendXmlNode);
                if (legendXmlNode.Attributes["visible"] != null)
                    mResultObject.IsVisible = ParseIsVisibleAttribute(legendXmlNode);
            } catch (DesignByContract.PreconditionException preExc) {
                //log error
                mResultObject = null;
                throw;
            }
        }

        private bool ParseIsVisibleAttribute(XmlNode legendXmlNode) {
            Check.Require(legendXmlNode.Attributes["visible"] != null, "visible attribute not present");
            return Boolean.Parse(legendXmlNode.Attributes["visible"].Value);
        }

        private LegendPosition ParsePositionAttribute(XmlNode legendXmlNode) {
            Check.Require(legendXmlNode.Attributes["position"] != null, "position attribute not present");
            return ConvertPositionFromName(legendXmlNode.Attributes["position"].Value.Trim());
        }

        private LegendPosition ConvertPositionFromName(string positionName) {
            Check.Require(positionName != null, "Position name not defined");

            LegendPosition legendPosition = LegendPosition.xlLegendPositionUnknown;
            switch (positionName) {
                case "xlLegendPositionBottom":
                    legendPosition = LegendPosition.xlLegendPositionBottom;
                    break;
                case "xlLegendPositionLeft":
                    legendPosition = LegendPosition.xlLegendPositionLeft;
                    break;
                case "xlLegendPositionRight":
                    legendPosition = LegendPosition.xlLegendPositionRight;
                    break;
                case "xlLegendPositionTop":
                    legendPosition = LegendPosition.xlLegendPositionTop;
                    break;
                case "xlLegendPositionCorner":
                    legendPosition = LegendPosition.xlLegendPositionCorner;
                    break;
                default:
                    break;
            }
            return legendPosition;
        }
    }
}